//---------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------- FLOOR ------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
	function equals(p1, p2){
//		('equals', "[",p1.x, p1.y,"]", "[",p2.x, p2.y,"]");
		
		var val = ((p1.x == p2.x) && (p1.y == p2.y));
			//if (val) console.log('match');
		return val;
	}	

	//retorna una version asociativa del arreglo de spaces
	function mapearArr(arr){
		arrAsoc = {}; //<id>--> indice
		for (var i = 0, l = arr.length; i < l; i++){
			var idSpace = arr[i].id;
			arrAsoc[idSpace] = i;
		}
		return arrAsoc;
	}
	
	//aux
	function esEscaleraEnU(contour, indice){
		var l = contour.length;
		//console.log('IS STAIR !!! space___ ->',_this.id, 'floor', _this.piso.id)
		//si la escalera es una 'U' no quiero generar la pared al costado de la escalera
		var indiceMenosUno = ((indice - 1)>= 0)? (indice -1) : (l -1);
		var indiceMasUno = (indice + 1) % l;
		var indiceMasDos = (indice + 2) % l;
		
		var precision = 3; //si los puntos no estan alineados totalmente
		
		var esEscaleraU = false; 
		
		if (      ( Math.abs(contour[indice].x - contour[indiceMenosUno].x) < precision)
				&& (Math.abs(contour[indiceMasUno].x - contour[indiceMasDos].x) < precision)){
			
			if (((contour[indice].y - contour[indiceMenosUno].y) >= 0 
				&& (contour[indiceMasUno].y - contour[indiceMasDos].y)>= 0)){
				esEscaleraU = true;
			}else if(((contour[indice].y - contour[indiceMenosUno].y) <= 0 
					&& (contour[indiceMasUno].y - contour[indiceMasDos].y) <= 0)){
				esEscaleraU = true;
			}
				
				
		}else if (        ( Math.abs(contour[indice].y - contour[indiceMenosUno].y) < precision)
				&& (Math.abs(contour[indiceMasUno].y - contour[indiceMasDos].y) < precision)){
			
			if (((contour[indice].x - contour[indiceMenosUno].x) >= 0 
				&& (contour[indiceMasUno].x - contour[indiceMasDos].x)>= 0)){
				esEscaleraU = true;
			}else if(((contour[indice].x - contour[indiceMenosUno].x) <= 0 
					&& (contour[indiceMasUno].x - contour[indiceMasDos].x) <= 0)){
				esEscaleraU = true;
			}
		}
		
		return esEscaleraU;
	}
	
	
//marco los puntos de los spaces como 'contorno'(nada) o 'portal'
	function preProcesarFloor(floor){
			
		var spaces = floor.spaces;
		var spacesAsoc = mapearArr(spaces);
		
		var portals = floor.portals; 
		var space = null;
		var indice = 0;
		
		for (var i = 0, l = portals.length; i < l; i++){
			var portal = portals[i];
			
			if (portal.class_ == "intra_floor" ){
				
				//source space
				indice =  spacesAsoc[portal.sourceID];
				space = spaces[ indice ];
				if (space){
					var contour = space.contour.points;
					
					for (var j = 0, l2 = contour.length; j< l2; j++){
						
						if (equals(contour[j], portal.p1)){
							contour[j].isPortal = true;
							if (portal.type == "ustair" || portal.type == "dstair"){
								contour[j].isStair = true;
								if (esEscaleraEnU(contour,j)){
									contour[j].isUStair = true;
									portal.isUStair = true;
								}
							}
							//console.log('match en p1')
							break;
						}else if(portal.p3){
							if (equals(contour[j], portal.p3)){
								contour[j].isPortal = true;
								if (portal.type == "ustair" || portal.type == "dstair"){
									contour[j].isStair = true;
									if (esEscaleraEnU(contour,j)){
										contour[j].isUStair = true;
										portal.isUStair = true;
									}
								}
								//console.log('match en p1')
								break;
							}
						}else{
							//console.log('portal intra floor (source) sin punto p3. portal: ',portal, 'piso', floor.floor.name)
						}
					}
				}else{
					console.log('space not found. intrafloor (source). arrAsoc',arrAsoc,' indice',indice,' portal: ',portal, 'piso', floor.floor.name)
				} 	
					
				//target space
				indice =  spacesAsoc[portal.targetID];
				space = spaces[ indice ];
				
				if (space){
					var contour = space.contour.points;
					
					for (var j = 0, l2 = contour.length; j< l2; j++){
						
						if (equals(contour[j], portal.p1)){
							contour[j].isPortal = true;
							if (portal.type == "ustair" || portal.type == "dstair"){
								contour[j].isStair = true;
								if (esEscaleraEnU(contour,j)){
									contour[j].isUStair = true;
									portal.isUStair = true;
								}
							}
							//console.log('match en p1')
							break;
						}else if(portal.p3){
							if (equals(contour[j], portal.p3)){
								contour[j].isPortal = true;
								if (portal.type == "ustair" || portal.type == "dstair"){
									contour[j].isStair = true;
									if (esEscaleraEnU(contour,j)){
										contour[j].isUStair = true;
										portal.isUStair = true;
									}
								}
								//console.log('match en p1')
								break;
							}
						}else{
							//console.log('portal intra floor (target) sin punto p3: ',portal, 'piso', floor.floor.name)
						}
					}
				}else{
					if (portal.targetID){
						console.log('space not found. intrafloor (target). arrAsoc',arrAsoc,' indice',indice,' portal: ',portal, 'piso', floor.floor.name)	
					}else{
						//console.log('space not found. intrafloor (target). portal.targetID es undefined')
					}
					
				} 
				
			}else if(portal.class_ == "inter_floor"){
				//target space
				indice =  spacesAsoc[portal.sourceID];
				space = spaces[ indice ];
				if (space){
					var contour = space.contour.points;
					
					for (var j = 0, l2 = contour.length; j< l2; j++){
						
						if (equals(contour[j], portal.p1)){
							//console.log('match en inter_floor')
							contour[j].isPortal = true;
							break;
						}
					}
				}else{
					//console.log('space not found in inter_floor portal. index: ',portal.sourceID, 'piso', floor.floor.name)
				}  
			}
		}//fin for
	}


//<piso> : data para crear el piso
	//
	//<return> : puntero al piso creado
	Piso = function (data){
		
		preProcesarFloor(data);
		//console.log('floor', data);	
		
		var dataFloor = data.floor;
		
		this.id = dataFloor.name; //(str) por ahora uso 'name' como ID
		this.name = dataFloor.name; //(str) nombre del piso
		
		this.tipo = "piso";
		this.level = dataFloor.level; //(num) nivel
		this.buildingName = dataFloor.buildingName;
		
		this.absoluteHeight = dataFloor.absoluteHeight;
		this.altura = 0; //Ale. deprecated? TODO
		
		//definimos el contorno
		this.contour = {};	
		
		//centoride del contorno
		this.contour.centroid = {};
		this.contour.centroid.x = dataFloor.contour.centroid.x;
		this.contour.centroid.y = 0;//TODO
		this.contour.centroid.z = dataFloor.contour.centroid.y;
		
		//puntos del contorno
		this.contour.points = [];
		
		var thisContour = this.contour.points;//aux
		var dataContour = dataFloor.contour.points;//aux
		for (var i = 0, l =  dataContour.length; i < l; i++){
			thisContour.push({x: dataContour[i].x, y: 0, z: dataContour[i].y}); //X=X, Y=Z, Z=Y
		}
			
		//spaces y portals
		this.spaces = {};
		this.portals = [];
		this.landmarks = [];
		
		//insertamos los SPACES
		var dataSpaces = data.spaces;//aux
		var id = null; //aux
		for (var i = 0, l = dataSpaces.length; i < l; i++){
			id = (USANDO_SPACES_ID)? dataSpaces[i].id : dataSpaces[i].name;
			this["spaces"][id] = new Space(dataSpaces[i], this);
		}

		this.referenceNodeIndex = dataFloor.referenceNode;//indice al space de referencia
		this.referenceNode = this.spaces[this.referenceNodeIndex];
		
		//insertamos los PORTALS
		//si son intra-floor, los creo
		//si son inter-floor, me fijo si ya existe y linkeo; y sino existe, lo creo
		if (data.portals && data.portals.length){
			var dataPortals = data.portals; //aux
			for (var i = 0, l = dataPortals.length; i < l; i++){
				switch (dataPortals[i].class_){
					case "intra_floor":
						this["portals"].push(new Portal(dataPortals[i], this)); 
						break;
					case "inter_floor":
						//esto ya no lo maneja el Floor, sino la jerarquia
						//ver implementacion en model_jerarquia_spaces.js
						//var sourceID = this.buildingName+"/"+this.name+"/"+dataPortals[i].sourceID; //armo el path absoluto
						//linkearPortalInterFloor(sourceID, data.portals[i], this);
						break;
				};
			};
		}
		
		
		//insertamos los LANDMARKS
		if (data.landmarks && data.landmarks.length){
			for (var i = 0, l = data.landmarks.length; i < l; i++){
			//	nuevoPiso["spaces"][data.spaces[i].name] = new Space(data.spaces[i], nuevoPiso);
				var landmark = new Landmark(data.landmarks[i], this);
				this["landmarks"].push(landmark);
			};
		}
		
		
		return this; //retorna el 'punterito' al piso recientemente creado
	};
	
	
	

	
	